#include "ChangeBookmarkCommand.h"

#include "BookmarksManager.h"
#include "BookmarkNode.h"

ChangeBookmarkCommand::ChangeBookmarkCommand(BookmarksManager *bookmarkManager, BookmarkNode *node,
                        const QString &newValue, ChangeBookmarkType type)
    : QUndoCommand()
    , m_bookmarkManager(bookmarkManager)
    , m_type(type)
    , m_newValue(newValue)
    , m_node(node)
{
    if (m_type == changedTitle) {
        m_oldValue = m_node->title;
        setText(BookmarksManager::tr("Name Change"));
    }
        if (m_type == changedURL)
    {
        m_oldValue = m_node->url;
        setText(BookmarksManager::tr("Address Change"));
    }
    else {
        m_oldValue = m_node->tags;
        setText(BookmarksManager::tr("Tags Change"));
    }
}

void ChangeBookmarkCommand::undo()
{
    if (m_type == changedTitle)
        m_node->title = m_oldValue;
    else
    if (m_type == changedTags)
        m_node->tags = m_oldValue;
    else
        m_node->url = m_oldValue;
    emit m_bookmarkManager->entryChanged(m_node);
}

void ChangeBookmarkCommand::redo()
{
    if (m_type == changedTitle)
        m_node->title = m_newValue;
    else
    if (m_type == changedTags)
        m_node->tags = m_newValue;
    else
        m_node->url = m_newValue;
    emit m_bookmarkManager->entryChanged(m_node);
}
